const PROJECT_PATH_KEY = 'PROJECT-PATH-KEY'
const storage = require('electron-json-storage')
function savePath(paths) {
    storage.set(PROJECT_PATH_KEY, paths, {}, (err) => {
        console.log('存储成功')
    })
}
export async function setProjectPath(path) {
    if (!path) {
        return
    }
    // let paths = await getProjectPath()
    let paths = getProjectPath()
    let len = paths.length
    for (let i = 0; i < len; i++) {
        if (path === paths[i]) {
            paths.splice(i, 1 )
        }
    }
    paths.push(path)
    savePath(paths)
}

export function getProjectPath() {
    console.log(storage.getDataPath())
    return storage.getSync(PROJECT_PATH_KEY)

    // return new Promise((resolve, reject) => {
    //     console.log(PROJECT_PATH_KEY)
    //     storage.get(PROJECT_PATH_KEY, {}, (err, data) => {
    //         console.log('获取路径回调')
    //         console.log(err)
    //         if (!err) {
    //             if (typeof data.length === 'undefined') {
    //                 data = []
    //             }
    //             resolve(data)
    //         } else {
    //             console.log('获取路径出错')
    //             console.log(err)
    //             reject([])
    //         }
    //     })
    // })
}

export function getLastProjectPath() {
    // let paths = await getProjectPath()
    let paths = getProjectPath()
    console.log(paths)
    let len = paths.length
    console.log(typeof paths)
    console.log('len===', len)
    return len > 0 ? paths[len - 1] : ''
}

export async function removeGitProject(path) {
    // let paths = await getProjectPath()
    let paths = getProjectPath()
    for (let key in paths) {
        if (paths[key] === path ) {
            paths.splice(key, 1)
        }
    }
    savePath(paths)
}

export default {
    setProjectPath,
    getProjectPath,
    getLastProjectPath,
    removeGitProject
}
